// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import api from './api'
import router from '../src/router/index'
import store from './store'
import iView from 'iview'
import VueAuth from '@websanova/vue-auth'
import 'iview/dist/styles/iview.css'   // 使用 CSS
import '../index.less'

import axios from 'axios'
import VueAxios from 'vue-axios'
// 桂
// axios.defaults.baseURL = 'http://192.168.1.141:8887'
// axios.defaults.baseURL = 'http://192.168.1.140:8887'
// 线上
axios.defaults.baseURL = '//www.jingbaohe.com/api'
axios.defaults.timeout = 8000
axios.interceptors.response.use(function (response) {
  return response;
}, function (error) {
  if (window.location.href.indexOf('/home') > -1) {
    if (error.response.status === 401) {
      window.location.href = window.location.origin + '/admin/login'
    }
  }
  return Promise.reject(error);
});
// axios.defaults.baseURL = '//106.75.143.162:8887'
Vue.http = Vue.prototype.$http = axios
Vue.use(iView)
Vue.use(VueAxios, axios)
Vue.use(api, {
  errorFunction(res) {
    // console.log('通用业务逻辑错误函数')
  },
  successFunction(res) {
    // console.log('通用成功函数')
  },
  apiErrorFunction(res) {
    // console.log('通用接口错误函数')
  }
})

Vue.config.productionTip = false
Vue.router = router
Vue.use(VueAuth, {
  auth: require('./packages/vue-auth/drivers/custom-bearer.js'),
  http: require('@websanova/vue-auth/drivers/http/axios.1.x.js'),
  router: require('@websanova/vue-auth/drivers/router/vue-router.2.x.js'),
  tokenName: 'access-token',
  refreshData: { url: 'auth/refresh', method: 'GET', enabled: false, interval: 0 },
  logoutData: { url: 'auth/logout', method: 'POST', redirect: '/admin/login', makeRequest: true },
  notFoundRedirect: '/admin/home',
  authRedirect: {path: '/admin/login'}
});

new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})
